package cn.tedu.weibo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
 * @author 作者：HeShaoShuai
 * @Email 邮箱：2569819541@qq.com
 * @Date 日期：2023/5/17 15:36
 * @Package 包名：cn.tedu.weibo.controller
 * @Project 项目名：JavaSE
 * @Version 1.0
 */
@RestController
public class UploadController {
	@RequestMapping("/upload")
	public String upload(MultipartFile picFile) throws IOException {
		String fileName = picFile.getOriginalFilename();
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		fileName = UUID.randomUUID()+suffix;
		//文件夹路径
		String dirPath = "d:/IDEA/files";
		//日期路径
		String datePath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
		File dirFile = new File(dirPath+datePath);
		//判断文件夹不存在 则创建
		if (!dirFile.exists()){
			dirFile.mkdirs();
		}
		//保存文件到指定文件夹  异常抛出
		picFile.transferTo(new File(dirPath+datePath+fileName));
		return datePath+fileName;
	}

	@RequestMapping("/remove")
	public void remove(String url){
		new File("d:/IDEA/files"+url).delete();
	}
}